package cn.edu.hncj.Config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * @Auther: Lix
 * @Date: 2024/11/17 - 11 - 17 - 13:49
 * Description: cn.edu.hncj.Config
 * Version: 1.0
 */
//<context:property-placeholder location="classpath:jdbc.properties"/>
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")     //Spring会将当前方法的返回值以指定名称存储到Spring容器中
    public DataSource getDataSource() throws Exception {
        ComboPooledDataSource cpd = new ComboPooledDataSource();
        cpd.setDriverClass(driver);
        cpd.setJdbcUrl(url);
        cpd.setUser(username);
        cpd.setPassword(password);
        return cpd;
    }
}
